package com.platform.service.impl;

import com.platform.cache.J2CacheUtils;
import com.platform.dao.UserDao;
import com.platform.dao.UserDeleteDao;
import com.platform.entity.TeamEntity;
import com.platform.entity.TeamWithUserEntity;
import com.platform.entity.UserAssetEntity;
import com.platform.entity.UserEntity;
import com.platform.entity.excel.UserExcel;
import com.platform.service.TeamService;
import com.platform.service.TeamWithUserService;
import com.platform.service.UserAssetService;
import com.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Service实现类
 * 会员管理:用户管理
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-16 15:02:28
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserAssetService userAssetService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private TeamWithUserService teamWithUserService;
    @Autowired
    private UserDeleteDao userDeleteDao;

    @Override
    public UserEntity queryObject(Long id) {
        return userDao.queryObject(id);
    }

    @Override
    public List<UserEntity> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userDao.queryTotal(map);
    }

    @Override
    @Transactional
    public int save(UserEntity user) {
        int i = userDao.save(user);
        if (i > 0) {
            TeamEntity team = new TeamEntity();
            team.setName("团队-" + user.getName());
            Integer tid = teamService.save(team);
            TeamWithUserEntity tu = new TeamWithUserEntity();
            tu.setParentTeamId(0l);
            tu.setSubTeamId(team.getId());
            tu.setUserId(user.getId());
            // // 创建邀请partner.getId(), 0, tid
            teamWithUserService.save(tu);
            UserAssetEntity userAsset = new UserAssetEntity();
            userAsset.setUid(user.getId());
            userAsset.setPhone(user.getMobile());
            userAsset.setUserName(user.getName());
            userAssetService.save(userAsset);
            return 1;
        }

        return 0;
    }

    @Override
    public int update(Long uid) {
        UserEntity user = this.queryObject(uid);
        int done = user.getIsdOne() == 0 ? 1 : 0;
        user.setIsdOne(done);
        int i = userDao.update(user);
        if (i > 0) {
            J2CacheUtils.remove("", "REDIS_USER_KEY::userId" + user.getId());
        }
        return i;
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return userDao.deleteBatch(ids);
    }

    @Override
    public int updateinfo(UserEntity tPartner) {
        int i = userDao.updateinfo(tPartner);
        if (i > 0) {
            J2CacheUtils.remove("", "REDIS_USER_KEY::userId" + tPartner.getId());
        }
        return i;
    }

    @Transactional
    @Override
    public int deleteUserData(Integer uid) {
        userDeleteDao.delUser(uid);
        userDeleteDao.delAsset(uid);
        userDeleteDao.delIncoin(uid);
        //userDeleteDao.delOrder(uid);
        //userDeleteDao.delCush(uid);
        userDeleteDao.delAddress(uid);
        userDeleteDao.delSport(uid);
        //userDeleteDao.delMerchantCoin(uid);
        userDeleteDao.delTeam(uid);
        userDeleteDao.delTeamUser(uid);
        userDeleteDao.delInvite(uid);
        return 1;
    }

    @Override
    public List<UserExcel> queryListExcel(String id, String name, String phone, Integer done, Integer frozen, Integer index, Integer size) {
        int start =(index-1)*size;
        return userDao.queryListExcel( id,  name,  phone,  done,  frozen,  start,  size);
    }

    @Override
    public UserEntity queryObjectByPhone(String phone) {
        return userDao.queryObjectByPhone(phone);
    }

    @Override
    public List<UserEntity> selectUserListForId(List<Long> uids) {

        return userDao.selectUserListForId(uids);
    }


}
